package com.Prot2.Logger;

import com.Prot2.Globals;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;

public class GPSWorker {
		
		private static final String TAG="GPSWorker";
		//private LocationManager mLocationManager;
		//private MListener mListener;
		private ContentResolver gpsManager;
		private LocationContentObserver gpsObserver;
		private LocationManager mLocationManager;
		private double longitude;
		private double latitude;
		private int gpsStatus;
		private Context mContext;
		
		
		public GPSWorker(Context c){
			mLocationManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
			//mLocationManager = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE);
			//mLocationManager.addGpsStatusListener(mListener);
			//Log.w(TAG,"GPS Listener started");
			mContext=c;
			
			getGPSStatus();
			
			gpsManager = c.getContentResolver();
			gpsObserver= new LocationContentObserver ( new Handler(),c);
			gpsManager.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), false, gpsObserver );
			Log.w(TAG,"GPS Observer started");
		}
		
		
		
		private LocationListener mLocationListener = new LocationListener() {
			
			public void onStatusChanged(String provider, int status, Bundle extras) {}
			public void onProviderEnabled(String provider) {}
			public void onProviderDisabled(String provider) {}
			
			public void onLocationChanged(Location location) {
				latitude=location.getLatitude();
				longitude=location.getLongitude();
				Log.w(TAG,"GPS latitude: "+latitude);
				Log.w(TAG,"GPS longitude "+longitude);
			}
			};
		
		class LocationContentObserver extends ContentObserver {
			
			
			  public LocationContentObserver( Handler h, Context c ) {
			    super( h );
			  }

			  public void onChange(boolean selfChange) {
				  Log.w(TAG,"GPS status changed!");
				  getGPSStatus();
				  mLocationManager.removeUpdates(mLocationListener);
				  switch(gpsStatus){
					case 0:
						Log.w(TAG,"GPS case 0: setting zero");
						longitude=0;
						latitude=0;
						break;
					case 1:
						Log.w(TAG,"GPS case 1: registering gps listener");
						mLocationManager.requestLocationUpdates("gps", Globals.samplingRate*1000, 0, mLocationListener);
						break;
					case 2:
						Log.w(TAG,"GPS case 2: registering network listener");
						mLocationManager.requestLocationUpdates("network", Globals.samplingRate*1000, 0, mLocationListener);
						break;
					case 3:
						Log.w(TAG,"GPS case 3: registering gps,network listener");
						mLocationManager.requestLocationUpdates("gps", Globals.samplingRate*1000, 0, mLocationListener);
						break;
				  }
				  
			  }
			}

		
		private void getGPSStatus(){
			  String providers = Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
			  Log.w(TAG,"GPS:"+providers);
			  gpsStatus=3;
			  if(providers.isEmpty()) {gpsStatus=0;}
			  if(providers.contains("gps")) {gpsStatus=1;}
			  if(providers.contains("network")) {gpsStatus=2;}
			  if(providers.contains("gps,network")) {gpsStatus=3;}
			  if(providers.contains("network,gps")) {gpsStatus=3;}

		
		}
		
		
		public String getRead(String DELIMITER){
			return ""+gpsStatus+DELIMITER+latitude+DELIMITER+longitude;
			}
			
		
		
/*		private class MListener implements GpsStatus.Listener {

			@Override
			public void onGpsStatusChanged(int event) {
				Log.w(TAG,"GPS event");
				
				switch(event){
				case GpsStatus.GPS_EVENT_STARTED:
					Log.w(TAG,"GPS started");
					break;
				case GpsStatus.GPS_EVENT_STOPPED:
					Log.w(TAG,"GPS stopped");
					break;
				}
				
			}
			
		}
*/		
		public void stop(){
			//mLocationManager.removeGpsStatusListener(mListener);
			//Log.w(TAG,"GPS Listener stopped");
			gpsManager.unregisterContentObserver(gpsObserver);
			Log.w(TAG,"GPS Observer stopped");
			mLocationManager.removeUpdates(mLocationListener);
		}

	
	}

